<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class User extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        
        $this->data['controller'] = 'user';         
        $this->load->model('Muser');
    }
    
    public function index()
    {
        if(@$this->Muser->checkManage($this->session->userdata('admin_id')) == 0)
        {
            exit('Bạn không có quyền');
        }
        $this->data['id_active'] = $this->getParamUri(3,2);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageg'] = $this->getParamUri(3,2);
        $data = $this->Muser->getUserDB($config['per_page'],$this->getParamUri(3,2));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,2);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
	    $this->data['action'] = 'index';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function adduser()
    {
        if(@$this->Muser->checkManage($this->session->userdata('admin_id')) == 0)
        {
            exit('<meta charset="utf-8">Bạn không có quyền');
        }
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tài khoản', 'required|xss_clean');
        $this->form_validation->set_rules('passwd', 'Mật khẩu', 'required|xss_clean');
        
        if ($this->form_validation->run() === TRUE)
        {
            $this->data['result'] = $this->Muser->adduserDB();
        }
        $this->data['action'] = 'adduser';
        $this->load->view('temp/layout', $this->data);
    }
    public function edituser()
    {
        if(@$this->Muser->checkManage($this->session->userdata('admin_id')) == 0)
        {
            exit('<meta charset="utf-8">Bạn không có quyền');
        }
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Muser->getUsser($this->data['id']);
        $this->load->library('form_validation');
        $this->form_validation->set_rules('id', 'Tài khoản', 'required|xss_clean');
        if ($this->form_validation->run() === TRUE)
        {
            $this->data['result'] = $this->Muser->edituserDB($this->data['id']);
        }
        $this->data['action'] = 'edituser';
        $this->load->view('temp/layout', $this->data);
    }
    public function logout()
    {
        $this->session->sess_destroy();
        redirect('manage');
    }
    
    public function deleteuser()
    {
        if(@$this->Muser->checkManage($this->session->userdata('admin_id')) == 0)
        {
            exit('<meta charset="utf-8">Bạn không có quyền');
        }
        $run = $this->Muser->deleteuserdb();
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa quản trị thành công");window.location.href="'.$this->data['baseurl'].'manage/user.html";</script>');
        }
        
    }
    
    public function login()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tài khoản', 'required|xss_clean');
        $this->form_validation->set_rules('passwd', 'Mật khẩu', 'required|xss_clean');
        
        if ($this->form_validation->run() === TRUE)
        {
            $result = $this->Muser->loginAdmin();
            if($result == 200)
            {
                redirect('manage');
            }else if($result == 201){
                $this->data['result'] = 'Sai mật khẩu';
            }else if($result == 202){
                $this->data['result'] = 'Vui lòng kiểm tra lại hoặc liên hệ <b>0982362911</b> để được trợ giúp';
            }
        }
        
        $this->load->view('temp/user/login', $this->data);
    }
    
    public function changepw()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('passwd', 'Mật khẩu cũ', 'required|xss_clean');
        
        if ($this->form_validation->run() === TRUE)
        {
            $this->data['result'] = $this->Muser->changepwDB($this->session->userdata('username'));
           // die('<meta charset="utf-8"><script>alert("Cập nhật thành công");window.location.href="'.$this->data['baseurl'].'manage/doi-mat-khau.html";</script>');
        }
        $this->data['action'] = 'changepw';
        $this->load->view('temp/layout', $this->data);
    }
    
}
